package com.fenghongzhang.day008;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 6.0版本,新增动态授权.1,清单, 2,代码(动态)
 *
 * 1.普通
 * 2,危险.
 *
 *
 * 1,是否挂载
 * 2,文件目录
 * 3,写. -> 3.1清单授权, 3.2 动态授权
 */
public class SDActivity extends AppCompatActivity {

    private Button read;
    private Button write;

    
    private static final String TAG = "SDActivity";


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        //并且同意
        if(requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "同意", Toast.LENGTH_SHORT).show();
        }else {
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s_d);

        read = (Button) findViewById(R.id.read);
        write = (Button) findViewById(R.id.write);


        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String externalStorageState = Environment.getExternalStorageState();
                if(externalStorageState.equals(Environment.MEDIA_MOUNTED)){
                    File externalStorageDirectory = Environment.getExternalStorageDirectory();
                    try {
                        FileInputStream fileInputStream = new FileInputStream(new File(externalStorageDirectory, "a.txt"));
                        byte[] bytes = new byte[1];
                        StringBuilder stringBuilder = new StringBuilder();
                        int len = 0;
                        while((len = fileInputStream.read(bytes)) != -1){
                            stringBuilder.append(new String(bytes, 0, len));

                        }
                        String s = stringBuilder.toString();

                        Toast.makeText(SDActivity.this, s, Toast.LENGTH_SHORT).show();

                    } catch (FileNotFoundException e) {


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });


        //版本判断
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
            //问一问
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
        }


        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String externalStorageState = Environment.getExternalStorageState();
                Log.i(TAG, "onClick: "+externalStorageState);

                File externalStorageDirectory = Environment.getExternalStorageDirectory();
                Log.i(TAG, "onClick: "+externalStorageDirectory);

                ///storage/emulated/0/a.txt
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(externalStorageDirectory, "a.txt"));

                    fileOutputStream.write("123123123".getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
